package com.snail.jj.androiddemos.components;


import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.snail.jj.androiddemos.R;

/**
 * Created by snail on 2017/5/11.  前台Service
 */

public class ForeheadService extends Service {


    private static final String TAG = "ForeheadService";
    private MyBinder myBinder = new MyBinder();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG,"onCreate");




    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");

        Notification.Builder builder = new Notification.Builder(this.getApplicationContext());
        Intent nfIntent = new Intent(this,ServiceActivity.class);

        builder.setContentIntent(PendingIntent.getActivity(this,0,nfIntent,0));
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle("标题");
        builder.setSmallIcon(R.mipmap.icon);
        builder.setContentText("内容");
        builder.setWhen(System.currentTimeMillis());

        Notification notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND;

        startForeground(110,notification);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
        Log.i(TAG,"onDestroy");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    class MyBinder extends Binder {
        public void startDownload() {
            Log.i("MyBinder", "start down somethings");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //执行具体的下载任务
                }
            }).start();


        }
    }
}